package com.ruoyi.erecord.locus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.erecord.locus.domain.bo.LocusStateBo;
import com.ruoyi.erecord.locus.domain.vo.LocusStateVo;
import com.ruoyi.erecord.locus.domain.LocusState;
import com.ruoyi.erecord.locus.mapper.LocusStateMapper;
import com.ruoyi.erecord.locus.service.ILocusStateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 轨迹-人员状态Service业务层处理
 *
 * @author cc
 * @date 2023-05-15
 */
@RequiredArgsConstructor
@Service
public class LocusStateServiceImpl implements ILocusStateService {

    private final LocusStateMapper baseMapper;

    /**
     * 查询轨迹-人员状态
     */
    @Override
    public LocusStateVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询轨迹-人员状态列表
     */
    @Override
    public TableDataInfo<LocusStateVo> queryPageList(LocusStateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LocusState> lqw = buildQueryWrapper(bo);
        Page<LocusStateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询轨迹-人员状态列表
     */
    @Override
    public List<LocusStateVo> queryList(LocusStateBo bo) {
        LambdaQueryWrapper<LocusState> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LocusState> buildQueryWrapper(LocusStateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LocusState> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), LocusState::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), LocusState::getDeviceCode, bo.getDeviceCode());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            LocusState::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增轨迹-人员状态
     */
    @Override
    public Boolean insertByBo(LocusStateBo bo) {
        LocusState add = BeanUtil.toBean(bo, LocusState.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
//            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改轨迹-人员状态
     */
    @Override
    public Boolean updateByBo(LocusStateBo bo) {
        LocusState update = BeanUtil.toBean(bo, LocusState.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LocusState entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除轨迹-人员状态
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
